﻿layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    form.on('radio(CashTypeTeam)', function (data) {
        //console.log(data.elem);
        //alert(data.value);//判断单选框的选中值
        //#sunmoney()# 计算总额也根据付款方式发生变化
        if (data.value == 2) {
            $("#CashModel").val(1);
            $("#panelCashModel2").show();
            $("#panelCashModel3").hide();

            $("#offLineExchangeRateText").show();
            $("#aliPayExchangeRateText").hide();

        } else if (data.value == 3) {
            $("#CashModel").val(2);
            $("#panelCashModel3").show();
            $("#panelCashModel2").hide();

            $("#offLineExchangeRateText").hide();
            $("#aliPayExchangeRateText").show();

        } 
        sumMoney($("#Money").val());
        $("#CashType").val(data.value);
    });

    function GetCurrentUserInfo() {
        $.ajax({
            type: 'GET',
            url: '/InOutCash/GetInCoinUserData/',
            data: {},
            dataType: "json",
            success: function (res) {// res为相应体,function为回调函数
                console.log(res);
                if (res.Success) {
                    var data = res.Data;
                    $("#Id").val(data.ManagerId);
                    $("#RmmfUserId").val(data.RmmfUserId);
                    
                    $("#txtUserName").val(data.UserName);
                    $("#txtTotalMoney").val(data.TotalMoney);
                    $("#txtBankCard").val(data.BankCard);
                    $("#txtExchangeRate").val(data.ExchangeRate);
                    $("#txtAliPayExchangeRate").val(data.AliPayExchangeRate);
                    
                    $("#txtExchangeRateText").text("1美元等于" + data.ExchangeRate + "人民币");
                    $("#aliPayExchangeRate").text("1美元等于" + data.AliPayExchangeRate + "人民币");
                    
                    // default
                    //sumMoney(100);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
    }
    GetCurrentUserInfo();

    form.on("submit(inCoin)", function (data) {
        //获取防伪标记
        $.ajax({
            type: 'POST',
            url: '/InOutCash/AddOrModifyInCoin/',
            data: {
                ManagerId: $("#Id").val(),  //主键
                RmmfUserId: $("#RmmfUserId").val(),  //主键
                CashType: $("#CashType").val(),
                CashModel: $("#CashModel").val(),
                Money: $("#inCashMoney").val() || $("#Money").val()
            },
            dataType: "json",
            headers: {
                "X-CSRF-TOKEN-yilezhu": $("input[name='AntiforgeryKey_yilezhu']").val()
            },
            success: function (res) {//res为相应体,function为回调函数
                if (res.ResultCode == 200) {
                    //var alertIndex = layer.alert(res.ResultMsg, { icon: 1 }, function () {
                    //    //layer.closeAll("iframe");
                    //    ////刷新父页面
                    //    //parent.location.reload();
                    //    top.layer.close(alertIndex);
                    //    location.href = "/InOutCash/ConfirmInCoin?inCoinId=" + res.Data;
                    //});
                    location.href = "/InOutCash/ConfirmInCoin?inCoinId=" + res.Data;
                } else {
                    layer.alert(res.ResultMsg, { icon: 5 });
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert('操作失败！！！' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus, { icon: 5 });
            }
        });
        return false;
    });

    form.verify({
        alipay: function (value, item) { //value：表单的值、item：表单的DOM对象
            var val = parseFloat(value);
            var inType = $('input[name=CashTypeTeam]:radio:checked').val();

            if (inType == 2) {
                return;
            }
            if (!isNaN(val)) {
                if (val <= 0 || val > 6000) {
                    return "单笔最高入金6000美元";
                }
            }
        }
    });  

    form.on('select(inCashMoney)', function (data) {
        var value = data.elem.value;
        if (!value) {
            $("#panelMoney").show();
            $("#Money").val("");
        } else {
            $("#panelMoney").hide();
        }
        sumMoney(value);
    });

    $("#Money").keyup(function () {
        sumMoney($(this).val());
    });

    function sumMoney(value) {
        var inType = $('input[name=CashTypeTeam]:radio:checked').val();

        var rate = parseFloat($("#txtExchangeRate").val());
        if (inType == 3){
            rate = parseFloat($("#txtAliPayExchangeRate").val());
        }
        var money = parseFloat(value);
        var total = 0;
        if (!isNaN(rate) && !isNaN(money)) {
            total = Math.round(rate * money * 100) /100;
        }
        $("#txtRMBMoney").val(total);
    }

});

